﻿using System;
using ProjectSurvival.System.Save;

namespace ProjectSurvival
{
    public class AchievementItem
    {
        public string Key { get; private set; }
        public string Name { get; private set;}
        public string Description { get;private set; }
        public bool Unlocked { get; set; }
        public string IconName { get; private set; }

        private Func<bool> _condition;
        private Action<AchievementItem> _onUnlocked;

        public AchievementItem WithKey(string key)
        {
            Key = key;
            return this;
        }
        public AchievementItem WithName(string name)
        {
            Name = name;
            return this;
        }
        public AchievementItem WithDescrpition(string description)
        {
            Description = description;
            return this;
        }
        public AchievementItem WithUnlocked(bool unlocked)
        {
            Unlocked = unlocked;
            return this;
        }
        public AchievementItem WithIconName(string iconName)
        {
            IconName = iconName;
            return this;
        }

        public AchievementItem Condition(Func<bool> condition)
        {
            _condition = condition;
            return this;
        }
        public AchievementItem OnUnlocked(Action<AchievementItem> onUnlocked)
        {
            _onUnlocked = onUnlocked;
            return this;
        }

        public bool ConditionCheck()
        {
            return _condition();
        }

        public AchievementItem Load(SaveSystem saveSystem)
        {
            Unlocked = saveSystem.LoadBool($"achievement_first_{Key}", false);
            return this;
        }

        public void Unlock(SaveSystem saveSystem)
        {
            Unlocked = true;
            saveSystem.SaveBool($"achievement_first_{Key}",true);
            _onUnlocked?.Invoke(this);
            AchievementSystem.OnAchievementUnlocked.Trigger(this);
        }
    }
}